package com.vanke.kotlin_mvp.taskdetail

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.design.widget.FloatingActionButton
import android.support.design.widget.Snackbar
import android.support.v4.app.Fragment
import android.view.*
import com.vanke.kotlin_mvp.R
import com.vanke.kotlin_mvp.addedit.AddEditTaskActivity
import com.vanke.kotlin_mvp.addedit.AddEditTaskFragment
import kotlinx.android.synthetic.main.taskdetail_fragment.*

/**
 * @name Kotlin_MVP
 * @package name：com.vanke.kotlin_mvp.taskdetail
 * @class describe
 * @anthor shiyuchuan
 * @time 2017/8/22 15:13
 */
class TaskDetailFragment : Fragment(), TaskDetailContract.View {


    override var presenter: TaskDetailContract.Presenter? = null

    override val isActive: Boolean
        get() = isAdded

    override fun onResume() {
        super.onResume()
        presenter?.start()
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val root = inflater.inflate(com.vanke.kotlin_mvp.R.layout.taskdetail_fragment, container, false)
        setHasOptionsMenu(true)
        activity.findViewById<FloatingActionButton>(R.id.fad_edit_task).setOnClickListener {
            presenter?.editTask()
        }
        return root
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val deletePressed = item.itemId == R.id.menu_delete
        if (deletePressed) presenter?.deleteTask()
        return deletePressed
    }

    override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater) {
        inflater.inflate(R.menu.taskdetail_fragment_menu, menu)
    }

    override fun setLoadingIndicator(active: Boolean) {
        if (active) {
            task_detail_title.text = ""
            task_detail_description.text = "LOADING"
        }
    }

    override fun showMissingTask() {
        task_detail_title.text = ""
        task_detail_description.text = "No data"
    }

    override fun showEditTask(taskId: String) {
        val intent = Intent(context, AddEditTaskActivity::class.java)
        intent.putExtra(AddEditTaskFragment.ARGUMENT_EDIT_TASK_ID,taskId)
        startActivityForResult(intent, REQUEST_EDIT_TASK)
    }

    override fun hideTitle() {
        task_detail_title.visibility = View.GONE
    }

    override fun showTitle(title: String) {
        with(task_detail_title) {
            visibility = View.VISIBLE
            text = title
        }
    }

    override fun hideDescription() {
        task_detail_description.visibility = View.GONE
    }

    override fun showDescription(description: String) {
        with(task_detail_description) {
            visibility = View.VISIBLE
            text = description
        }
    }

    override fun showCompletionStatus(complete: Boolean) {
        with(task_detail_complete) {
            isChecked = complete
            setOnCheckedChangeListener { button, isChecked ->
                if (isChecked) {
                    presenter?.completeTask()
                } else {
                    presenter?.activateTask()
                }
            }
        }
    }

    override fun showTaskDeleted() {
        activity.finish()
    }

    override fun showTaskMarkedCompleted() {
        view?.let {
            Snackbar.make(it, "task marked complete", Snackbar.LENGTH_LONG).show()
        }
    }

    override fun showTaskMarkedActive() {
        view?.let {
            Snackbar.make(it, "task marked active", Snackbar.LENGTH_LONG).show()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == REQUEST_EDIT_TASK) {
            if (resultCode == Activity.RESULT_OK) {
                activity.finish()
            }
        }
    }

    companion object {
        private val ARGUMENT_TASK_ID = "TASK_ID"

        private val REQUEST_EDIT_TASK = 1

        fun newInstance(taskId: String?): TaskDetailFragment {
            val fragment = TaskDetailFragment().apply {
                arguments = Bundle().apply {
                    putString(ARGUMENT_TASK_ID, taskId)
                }
            }
            return fragment
        }
    }
}